import org.springframework.beans.factory.BeanFactoryAware
import org.springframework.beans.factory.BeanFactory
import org.springframework.beans.BeansException

class ManageService implements BeanFactoryAware {
    static transactional = false
    
    def indexTaskExecutor
    def beanFactory
    
    void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory
    }
    
    def recreateIndex() {

        def entries = Entry.findAll()
        
        def queue = indexTaskExecutor.createQueue(entries.size)
        
        entries.eachWithIndex { entry, idx ->
            def process = beanFactory.getBean('indexProcess')
            process.entry = entry
            indexTaskExecutor.execute(process)
            if (idx % 100 == 0) {
                Thread.sleep(1000)
            }
        }
        
        while (queue.size() > 0) {
            Thread.sleep(1000)
        }
    }
    

}